﻿var isSuggest = false;
function suggest(obj, objId, e) {
	if(isSuggest == false){
		return "";
	}
  var fn = "if(typeof(SearchSuggest" + objId + ") == \"undefined\"){";
  fn += "SearchSuggest" + objId + " = new SearchSuggest(obj,objId);";
  fn += "}";
  eval(fn);
  var tempType = 1;
  eval("SearchSuggest" + objId).doSuggest(obj, e, tempType, 10);
}
function SearchSuggest(obj, objId) {
  this.isFirst = true;
  this.keyWord = "";
  this.lastKeyWord = "";
  this.suggestContainerId = "";
  this.suggestTableId = "";
  this.iframeId = "";
  this.selectedIndex = -1;
  this.keyCode = -1;
  this.keyType = -1;
  //0 down 1 up 2 enter <2 other 
  this.suggestListStatus = 0;
  //0 no list 1 list hidden 2 list show 3 mouse move on the list
  this.htmlStr = "";
  this.suggestContainerClass = "";
  this.suggestTableClass = "";
  this.suggestOverClass = "";
  this.inputObj = obj;
  this.inputId = objId;
  this.obj = this;
  this.cacheAll = new jcache(100);
  this.cacheTitle = new jcache(100);
  this.cacheAuthor = new jcache(100);
  this.cacheTags = new jcache(100);
  this.searchField = "";
  if (this.isFirst) {
    this.init();
  }
}
SearchSuggest.prototype.doSuggest = function(obj, e, type, num) {
  e = e ? e: (window.event ? window.event: null);
  this.keyCode = e.keyCode || e.which || e.charCode;
  this.keyWord = $.trim((obj.value.toLowerCase()));
  this.searchField = document.getElementById("isearch").sf.value;
  var jsonparameter = {
    query: this.keyWord,
    sf: this.searchField,
    maxresults: num
  };
  if (this.keyWord == "") {
    $("#" + this.suggestContainerId).html("");
  } else {
    if (this.keyCode == 38) {
      if (this.suggestListStatus == 0) {
        this.getData(jsonparameter);
      }
      this.keyType = 0;
      this.upAndDownSelect();
    } else {
      if (this.keyCode == 40) {
        if (this.suggestListStatus == 0) {
          this.getData(jsonparameter);
        }
        this.keyType = 1;
        this.upAndDownSelect();
      } else {
        if (this.keyCode == 13 && this.keyType != 0 && this.keyType != 1 && this.suggestListStatus == 3) {
          this.keyType = 2;
          this.enterSelect();
        } else {
          this.getData(jsonparameter);
        }
      }
    }
  }
};
SearchSuggest.prototype.init = function() {
  var temp = new Date();
  this.suggestContainerId = "iC" + temp.getTime();
  this.suggestTableId = "iT" + temp.getTime();
  this.iframeId = "iF" + temp.getTime();
  this.suggestContainerClass = "clist" + temp.getTime();
  this.suggestTableClass = "ctable" + temp.getTime();
  this.suggestOverClass = "sman_selectedStyle" + temp.getTime();
  var cssStr = "";
  cssStr += "<style>\n";
  cssStr += "." + this.suggestContainerClass + " {height:300px;width:300px; font-size: 12px;cursor: default;position: absolute;padding: 0px;margin: 0px;display: none;}\n";
  cssStr += "." + this.suggestTableClass + " {border:1px red dashed; background: #FFFFFF;border: 1px solid #7F9DB9;font-size: 12px;cursor: default;position: absolute; padding: 0px;margin: 0px;}\n";
  cssStr += "." + this.suggestOverClass + " {font-size: 12px;background-Color: #102681; color: #FFFFFF;}\n";
  cssStr += "</style>\n";
  $("body").append(cssStr);
  var htmlTemp = "";
  htmlTemp += "<div id=\"" + this.suggestContainerId + "\" class='" + this.suggestContainerClass + "'></div>";
  $("body").append(htmlTemp);
  $("#" + this.suggestContainerId).css({
    left: getAbsoluteLeft(this.inputObj),
    top: getAbsoluteTop(this.inputObj) + getAbsoluteHeight(this.inputObj),
    width: getAbsoluteWidth(this.inputObj),
    display: "none"
  });
  var suggestObj = this.obj;
  $(this.inputObj).blur(function() {
    suggestObj.suggestListStatus = 1;
    $("#" + suggestObj.suggestContainerId).hide();
  });
  this.isFirst = false;
};
SearchSuggest.prototype.getData = function(jsonparameter) {
  if (this.lastKeyWord != this.keyWord) {
    $("#" + this.suggestContainerId).show();
    this.lastKeyWord = this.keyWord;
    // check it has cached
    if (this.searchField == "1") {
      if (this.cacheAll.exist(this.keyWord)) {
        var jsonCacheData = this.cacheAll.get(this.keyWord);
        this.doit(jsonCacheData);
      } else {
        var tempObj = this.obj;
        $.getJSON("/art_sc/searchsuggest.php?r=" + new Date().getTime(), jsonparameter,
        function(json) {
          tempObj.doit(json);
          //cache data
          tempObj.cacheAll.put(tempObj.keyWord, json);
        });
      }
    } else {
      if (this.searchField == "2") {
        if (this.cacheTitle.exist(this.keyWord)) {
          var jsonCacheData = this.cacheTitle.get(this.keyWord);
          this.doit(jsonCacheData);
        } else {
          var tempObj = this.obj;
          $.getJSON("/art_sc/searchsuggest.php?r=" + new Date().getTime(), jsonparameter,
          function(json) {
            tempObj.doit(json);
            //cache data
            tempObj.cacheTitle.put(tempObj.keyWord, json);
          });
        }
      } else {
        if (this.searchField == "3") {
          if (this.cacheAuthor.exist(this.keyWord)) {
            var jsonCacheData = this.cacheAuthor.get(this.keyWord);
            this.doit(jsonCacheData);
          } else {
            var tempObj = this.obj;
            $.getJSON("/art_sc/searchsuggest.php?r=" + new Date().getTime(), jsonparameter,
            function(json) {
              tempObj.doit(json);
              //cache data
              tempObj.cacheAuthor.put(tempObj.keyWord, json);
            });
          }
        } else {
          if (this.searchField == "4") {
            if (this.cacheTags.exist(this.keyWord)) {
              var jsonCacheData = this.cacheTags.get(this.keyWord);
              this.doit(jsonCacheData);
            } else {
              var tempObj = this.obj;
              $.getJSON("/art_sc/searchsuggest.php?r=" + new Date().getTime(), jsonparameter,
              function(json) {
                tempObj.doit(json);
                //cache data
                tempObj.cacheTags.put(tempObj.keyWord, json);
              });
            }
          }
        }
      }
    }
  }
};
SearchSuggest.prototype.doit = function(json) {
  this.htmlStr = "<iframe id=\"" + this.iframeId + "\" style=\"position:absolute;z-index:-1;width:100%;filter:alpha(opacity=0);-moz-opacity:0;scrolling:no;\" frameborder=\"0\" src=\"about:blank\"></iframe>" + "\n";
  this.htmlStr += "<table id=\"" + this.suggestTableId + "\" class=\"" + this.suggestTableClass + "\" width=\"100%\" border=0 cellspacing=0>" + "\n";
  var tempObj = this.obj;
  $.each(json,
  function(i) {
    tempObj.eachItem(json, i);
  });
  this.htmlStr += "</table>";
  $("#" + this.suggestContainerId).html(this.htmlStr);
  this.suggestListStatus = 2;
  this.selectedIndex = -1;
  $("#" + this.iframeId).css({
    width: getAbsoluteWidth(this.suggestTableId)
  });
  $("#" + this.suggestContainerId).show();
};
SearchSuggest.prototype.eachItem = function(json, i) {
  this.htmlStr += "<tr onmouseover='SearchSuggest.over(this,\"" + this.suggestTableId + "\",\"" + this.suggestOverClass + "\")' onmouseout='SearchSuggest.out(this,\"" + this.suggestTableId + "\")' onmousedown='SearchSuggest.down(\"" + this.inputId + "\",\"" + json[i].keyword + "\");'><td style='display:none;'>" + json[i].keyword + "</td><td align=\"left\" nowrap>" + json[i].keyword.replace(this.keyWord, "<font color=red style='font-weight:bold;'>" + this.keyWord + "</font>") + "</td><td align=\"right\" nowrap style=\"color:green;\"> \u7ea6 " + json[i].num + " \u7ed3\u679c</td></tr>" + "\n";
};
SearchSuggest.prototype.enterSelect = function() {
  var tableObj = $("#" + this.suggestTableId)[0];
  var maxIndex = tableObj.rows.length - 1;
  if (0 <= this.selectedIndex && this.selectedIndex <= maxIndex) {
    $(this.inputObj).val(tableObj.rows[this.selectedIndex].cells[0].innerHTML);
  }
};
SearchSuggest.prototype.upAndDownSelect = function() {
  if (this.suggestListStatus != 0) {
    $("#" + this.suggestContainerId).show();
    if (this.keyType == 1) {
      this.selectedIndex++;
    } else {
      if (this.keyType == 0) {
        this.selectedIndex--;
      }
    }
    var tableObj = document.getElementById(this.suggestTableId);
    var maxIndex = tableObj.rows.length - 1;
    if (this.selectedIndex < 0) {
      this.selectedIndex = maxIndex;
    }
    if (this.selectedIndex > maxIndex) {
      this.selectedIndex = 0;
    }
    $("#" + this.suggestTableId + " tr").removeClass();
    $(tableObj.rows[this.selectedIndex]).addClass(this.suggestOverClass);
    $(this.inputObj).val(tableObj.rows[this.selectedIndex].cells[0].innerHTML);
    this.suggestListStatus = 3;
  }
};
SearchSuggest.over = function(obj, tableId, overClass) {
  $("#" + tableId + " tr").removeClass();
  $(obj).addClass(overClass);
};
SearchSuggest.out = function(obj, tableId) {
  $("#" + tableId + " tr").removeClass();
};
SearchSuggest.down = function(inputId, keyword) {
  $("#" + inputId).val(keyword);
};
function getAbsoluteHeight(oBOrId) {
  var ob;
  if (typeof(oBOrId) == "string") {
    ob = document.getElementById(oBOrId);
  } else {
    if (typeof(oBOrId) == "object") {
      ob = oBOrId;
    }
  }
  return ob.offsetHeight;
}
function getAbsoluteWidth(oBOrId) {
  var ob;
  if (typeof(oBOrId) == "string") {
    ob = document.getElementById(oBOrId);
  } else {
    if (typeof(oBOrId) == "object") {
      ob = oBOrId;
    }
  }
  return ob.offsetWidth;
}
function getAbsoluteLeft(oBOrId) {
  var s_el = 0;
  var ob;
  if (typeof(oBOrId) == "string") {
    ob = document.getElementById(oBOrId);
  } else {
    if (typeof(oBOrId) == "object") {
      ob = oBOrId;
    }
  }
  el = ob;
  while (el) {
    s_el = s_el + el.offsetLeft;
    el = el.offsetParent;
  }
  return s_el;
}
function getAbsoluteTop(oBOrId) {
  var s_el = 0;
  var ob;
  if (typeof(oBOrId) == "string") {
    ob = document.getElementById(oBOrId);
  } else {
    if (typeof(oBOrId) == "object") {
      ob = oBOrId;
    }
  }
  el = ob;
  while (el) {
    s_el = s_el + el.offsetTop;
    el = el.offsetParent;
  }
  return s_el;
}